package tencent

import (
	"strings"
)

func longestCommonPrefixV1(strs []string) string {
	if len(strs) == 0 {
		return ""
	}
	str := strs[0]
	for i := 1; i < len(strs); i++ {
		if len(strs[i]) < len(str) {
			str = strs[i]
		}
	}

	for i := 0; i < len(str); i++ {
		tmp := str[0 : len(str)-i]
		find := true
		for j := 0; j < len(strs); j++ {
			if !strings.HasPrefix(strs[j], tmp) {
				find = false
				break
			}
		}
		if find {
			return tmp
		}
	}
	return ""
}

func longestCommonPrefix(strs []string) string {
	if len(strs) == 0 {
		return ""
	}
	if len(strs) == 1 {
		return strs[0]
	}
	loop := 0
	for ; loop < len(strs[0]); loop++ {
		isFinish := false
		for i := 1; i < len(strs); i++ {
			if loop >= len(strs[i]) {
				isFinish = true
				break
			}
			if strs[0][loop] != strs[i][loop] {
				isFinish = true
				break
			}
		}
		if isFinish {
			break
		}
	}

	return strs[0][0:loop]
}
